using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Media;
using Common;
using Common.Enum;
using Common.NotifyEvent;

namespace SSettings
{
    public class PrintSetting : NotifyPropertyChanged
    {
        private bool _ColorPrinter = true;

        private ESeriesType[] _CurveStyle = new ESeriesType[6];

        private SolidColorBrush[] _CurveBrush = new SolidColorBrush[6];

        public bool ColorPrinter
        {
            get
            {
                return _ColorPrinter;
            }
            set
            {
                if (_ColorPrinter != value)
                {
                    _ColorPrinter = value;
                    OnPropertyChanged("ColorPrinter");
                }
            }
        }

        public ESeriesType[] CurveStyle
        {
            get
            {
                return _CurveStyle;
            }
            set
            {
                _CurveStyle = value;
            }
        }

        public SolidColorBrush[] CurveBrush
        {
            get
            {
                return _CurveBrush;
            }
            set
            {
                _CurveBrush = value;
            }
        }

        public PrintSetting()
        {
            for (int i = 0; i < CurveStyle.Count(); i++)
            {
                CurveStyle[i] = ESeriesType.None;
                CurveBrush[i] = new SolidColorBrush(Utility.GetColor("ColorChannel" + (i + 1)));
            }
        }

        public PrintSetting Clone()
        {
            PrintSetting printSetting = new PrintSetting();
            StreamEx streamEx = new StreamEx();
            SaveToStream(streamEx);
            streamEx.Seek(0L, SeekOrigin.Begin);
            printSetting.ReadFromStream(streamEx);
            return printSetting;
        }

        public void SaveToStream(StreamEx stream)
        {
            stream.Write(PropertyKey.Key_PrintSetting);
            List<PropertyKey> list = new List<PropertyKey>();
            List<byte[]> list2 = new List<byte[]>();
            list.Add(PropertyKey.Key_ColorPrinter);
            list2.Add(BytesConverter.GetBytes(ColorPrinter));
            list.Add(PropertyKey.Key_PrintCurveStyle);
            StreamEx streamEx = new StreamEx();
            for (int i = 0; i < CurveStyle.Count(); i++)
            {
                streamEx.Write((int)CurveStyle[i]);
            }
            streamEx.Seek(0L, SeekOrigin.Begin);
            list2.Add(streamEx.ToArray());
            streamEx.Close();
            list.Add(PropertyKey.Key_PrintCurveColor);
            streamEx = new StreamEx();
            for (int j = 0; j < CurveStyle.Count(); j++)
            {
                streamEx.Write(CurveBrush[j].Color.ToString());
            }
            streamEx.Seek(0L, SeekOrigin.Begin);
            list2.Add(streamEx.ToArray());
            streamEx.Close();
            stream.Write(PropertyKey.Key_Index);
            stream.WriteIndex(list, list2);
            stream.Flush();
        }

        public bool ReadFromStream(StreamEx stream)
        {
            PropertyKey propertyKey = stream.ReadKey();
            if (propertyKey != PropertyKey.Key_PrintSetting)
            {
                stream.Seek(-4L, SeekOrigin.Current);
                return false;
            }
            propertyKey = stream.ReadKey();
            if (propertyKey == PropertyKey.Key_Index)
            {
                List<PropertyKey> keys;
                List<byte[]> dates;
                stream.ReadIndex(out keys, out dates);
                for (int i = 0; i < keys.Count; i++)
                {
                    switch (keys[i])
                    {
                        case PropertyKey.Key_ColorPrinter:
                            ColorPrinter = BytesConverter.BytesToBool(dates[i]);
                            break;
                        case PropertyKey.Key_PrintCurveStyle:
                            {
                                StreamEx streamEx = new StreamEx();
                                streamEx.Write(dates[i]);
                                streamEx.Seek(0L, SeekOrigin.Begin);
                                for (int k = 0; k < CurveStyle.Count(); k++)
                                {
                                    CurveStyle[k] = (ESeriesType)streamEx.ReadInt();
                                }
                                streamEx.Close();
                                break;
                            }
                        case PropertyKey.Key_PrintCurveColor:
                            {
                                StreamEx streamEx = new StreamEx();
                                streamEx.Write(dates[i]);
                                streamEx.Seek(0L, SeekOrigin.Begin);
                                for (int j = 0; j < CurveStyle.Count(); j++)
                                {
                                    CurveBrush[j] = new SolidColorBrush((Color)ColorConverter.ConvertFromString(streamEx.ReadString()));
                                }
                                streamEx.Close();
                                break;
                            }
                    }
                }
            }
            return true;
        }
    }
}
